package wiki.tools.vidition.droid;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import wiki.tools.vidition.droid.util.DbHelper;
import wiki.tools.vidition.droid.util.bean.RecommednBean;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ShowHighDetail extends Activity {

	private ListView listView;
	private DbHelper mDbHelper;
	private ArrayList<RecommednBean> high;
	private int index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.high);
		mDbHelper = new DbHelper(this);
		mDbHelper.open();
		listView = (ListView) findViewById(R.id.high_list);
		new Thread(runnable).start();
	}

	Runnable runnable = new Runnable() {
		public void run() {
			refreshData();
		}
	};

	private void refreshData() {
		high = mDbHelper.getAllHigh();
		handler.sendEmptyMessage(0);
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				if (0 == high.size()) {
					listView.setAdapter(null);
					new AlertDialog.Builder(ShowHighDetail.this)
							.setTitle("Tip").setMessage("History is null")
							.setPositiveButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											ShowHighDetail.this.finish();
										}
									}).show();
				} else {
					sort(high);
					String[] s = new String[high.size()];

					for (int i = 0; i < high.size(); i++) {
						s[i] = high.get(i).title;
					}

					ArrayAdapter<String> adapter = new ArrayAdapter<String>(
							ShowHighDetail.this,
							android.R.layout.simple_list_item_1, s);
					listView.setClickable(true);
					listView.setAdapter(adapter);
					listView.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							String time = String.valueOf(System
									.currentTimeMillis());
							RecommednBean bean = new RecommednBean();
							bean.title = high.get(position).title;
							bean.time = time;
							bean.count = high.get(position).count + 1;
							mDbHelper.updateOneHigh(bean);
							Intent intent = new Intent(ShowHighDetail.this,
									SearchRSS.class);
							intent.putExtra(Intent.EXTRA_TITLE, high
									.get(position).title);
							startActivityForResult(intent, 8888);
						}
					});
					listView
							.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

								public void onCreateContextMenu(
										ContextMenu menu, View v,
										ContextMenuInfo menuInfo) {
									// TODO Auto-generated method stub
									index = v.getTop();
									menu.setHeaderTitle("Delete");
									menu.add(0, 0, 0, "Delete one");
									menu.add(0, 1, 0, "Delete all");
								}
							});
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case 0:
			mDbHelper.deleteHigh(high.get(index).title);
			break;

		case 1:
			mDbHelper.deleteAllHigh();
			break;
		}
		refreshData();
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		refreshData();
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void sort(ArrayList<RecommednBean> list) {

		Collections.sort(list, new Comparator<RecommednBean>() {
			public int compare(RecommednBean object1, RecommednBean object2) {
				int count = object2.count - object1.count;
				if (count != 0)
					return count;
				return object2.time.compareTo(object2.time);
			}
		});
	}

	@Override
	protected void onDestroy() {
		mDbHelper.close();
		super.onDestroy();
	}

}